package com.myh.cc.coom.recommend.viewmodel

import android.content.Intent
import android.view.View
import com.blankj.utilcode.utils.LogUtils.i
import com.myh.cc.base.BaseModel
import com.myh.cc.coom.recommend.adapter.GlideImageLoader
import com.myh.cc.coom.recommend.adapter.RecommendAdapter
import com.myh.cc.coom.recommend.bean.BannerBean
import com.myh.cc.coom.recommend.bean.RecommendBean
import com.myh.cc.coom.recommend.net.RecommendNetControl
import com.myh.cc.coom.recommend.view.RecommendDetailsActivity
import com.myh.cc.coom.recommend.view.RecommendFragment
import com.myh.cc.databinding.FragmentRecommendBinding
import com.myh.cc.util.ConstantUtil
import com.myh.cc.widget.OnItemClickListener

/**
 * des    ：
 * create    ：  mayahao.
 * date   ：  2018/1/11.   ：
 */
class RecommendModel: BaseModel<FragmentRecommendBinding,RecommendNetControl>(),OnItemClickListener {

    lateinit var recommendBean:RecommendBean
    lateinit var fragment: RecommendFragment

    override fun onCreate() {
        super.onCreate()
        fragment = UI as RecommendFragment
    }

    override fun onBeforeRequest(tag: Int) {
    }

    override fun onSuccess(data: Any, tag: Int) {

         // val bean =  data as RecommendBean
         // 如果返回code 成功,初始化RecycleView
        when(tag){
            ConstantUtil.requestTagOne -> {
                recommendBean = data as RecommendBean
                initRecycleView(recommendBean)
            }
            ConstantUtil.requestTagTwo -> {
                initBanner(data as BannerBean)
            }

        }


    }

    override fun onError(errorMag: String, tag: Int) {
        i(errorMag)
    }


    /**
     * 初始化RecycleView
     */
    private fun initRecycleView(bean: RecommendBean){
        val  adapter = RecommendAdapter(bean,getContext())
        mBinder.recommendRv.adapter = adapter
        adapter.mListener = this

    }

    /**
     * 请求接口
     */
    fun getRecommendData() {
        //notice=0&g_tk=701075963&uin=0&platform=h5&needNewCode=1&_=1515729721762
        // format=json&cmd=firstpage&page=0&pagesize=20&sort=1&language=-1&genre=0&year=1&pay=0&type=-1&company=-1
        var map = HashMap<String,String>()
        map.put("format","json")
        map.put("cmd","firstpage")
        map.put("page","0")
        map.put("pagesize","20")
        map.put("sort","1")
        map.put("language","-1")
        map.put("year","1")
        map.put("pay","0")
        map.put("type","-1")
        map.put("company","-1")


        mControl.getRecommendData(this,"",map,ConstantUtil.requestTagOne)

    }

    /**
     * S
     */
    fun getBanner(){
        var map = HashMap<String,String>()
        map.put("format","json")
        mControl.getBanner(this,"",map,ConstantUtil.requestTagTwo)

    }

    private fun initBanner(bean: BannerBean){

        var images = ArrayList<String>()
        for (url in bean.data.slider){
            images.add(url.picUrl)
        }


        // 设置图片加载器
        mBinder.recommendBanner.setImageLoader(GlideImageLoader())
        // 设置图片集合
        mBinder.recommendBanner.setImages(images);
        // banner 设置方法全部调用完毕时 最后调用
        mBinder.recommendBanner.start()
    }

    override fun onClick(view: View, position: Int) {
        val  intent = Intent(getContext(), RecommendDetailsActivity::class.java)
        intent.putExtra("albummid",recommendBean.data.albumlist[position].album_mid)
        fragment.startActivity(intent)
    }

    override fun onItemLongClick(view: View, position: Int) {

    }
}